
cc.Class({
    extends: cc.Component,

    properties: {
        /**上方管子最小高度 */
        topPipeMinHeight:100,
        /**下方管子最小高度  */
        bottomPipeMinHeight:100,
        /**上、下管子垂直间距最小值 */
        spacingMinValue:250,
        /**上、下管子垂直间距最大值 */
        spacingMaxValue:300,
        /**上方管子节点 */
        topPipe:cc.Node,
        /**下方管子节点 */
        bottomPipe:cc.Node

    },

    init(pipeManager){
        this.pipeManager = pipeManager;
        this.initPositionX();
        this.initPositionY();
    },
    initPositionX(){
        let visibleSize = cc.director.getVisibleSize();//场景可见区域大小
        let sceneLeft = -visibleSize.width / 2;
        let sceneRight = visibleSize.width / 2;
        this.node.x = sceneRight+300;
        this.recylceX = sceneLeft - Math.max(this.topPipe.width,this.bottomPipe.width);//计算回收管道时的坐标
    },
    initPositionY(){
        let visibleSize = cc.director.getVisibleSize();
        let topPipeMaxY = visibleSize.height/2-this.topPipeMinHeight;
        let bottomPipeMinY = cc.find("Canvas/ground").y+this.bottomPipeMinHeight;
        let spacing = this.spacingMinValue+Math.random()*(this.spacingMaxValue-this.spacingMinValue);
        this.topPipe.y = topPipeMaxY-Math.random()*(topPipeMaxY-bottomPipeMinY-spacing);
        this.bottomPipe.y = this.topPipe.y-spacing;
    },
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    update (dt) {
        if(!this.pipeManager.isRunning){
            return
        }
        //实时更新管道位置
        this.node.x+=this.pipeManager.pipeMoveSpeed*dt;
        if(this.node.x<this.recylceX){
            this.pipeManager.recyclePipe(this)
        }
    },
});
